<template>
  <div class="top-title">
    <div class="top-title__left">
      <yk-title :level="3" class="top-title__heading">{{ props.name }}</yk-title>
      <slot name="custom" />
    </div>
    <div class="top-title__right" v-if="isSearch">
      <div class="search-container" :class="{ 'has-value': SearchData, 'is-searching': isSearchFocused }">
        <yk-input-search class="search-input" placeholder="搜索文章..." v-model="SearchData" @search="search"
          @focus="isSearchFocused = true" @blur="isSearchFocused = false">
          <template #suffix>
            <button class="search-button" @click="search">
              <IconSearchOutline />
            </button>
          </template>
        </yk-input-search>
        <button class="cancel-button" v-show="SearchData" @click="cancleSearch">
          <span class="cancel-icon">×</span>
          <span class="cancel-text">取消</span>
        </button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'

const SearchData = ref();
const isSearchFocused = ref(false);

type titleProps = {
  name: string;
  isSearch: boolean;
}
const props = withDefaults(defineProps<titleProps>(), {
  name: '总览',
  isSearch: true,
})

const emit = defineEmits(['search'])

//搜索事件
const search = () => {
  emit('search', SearchData.value)
}
//取消搜索
const cancleSearch = () => {
  emit('search', '')
  SearchData.value = ''
}

</script>

<style lang="less" scoped>
// 颜色变量
@primary-color: #3a7bd5;
@primary-light: #5a93e1;
@primary-dark: #2a65b8;
@text-color: #333;
@light-text: #666;
@border-color: #e9e9e9;
@bg-hover: #f5f7fa;
@transition-time: 0.3s;

.top-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 20px 0;
  margin-bottom: 28px;
  position: relative;

  &::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(to right,
        rgba(58, 123, 213, 0.5),
        rgba(58, 123, 213, 0.1) 60%,
        transparent);
  }

  &__left {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 16px;
  }

  &__heading {
    margin: 0;
    color: @text-color;
    font-weight: 700;
    position: relative;
    padding-left: 16px;
    letter-spacing: -0.3px;

    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      height: 70%;
      width: 4px;
      border-radius: 2px;
      background: linear-gradient(to bottom, @primary-color, @primary-light);
      box-shadow: 0 2px 6px rgba(58, 123, 213, 0.3);
    }
  }

  &__right {
    flex-shrink: 0;
  }
}

.search-container {
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all @transition-time;

  .search-input {
    width: 220px;
    transition: all @transition-time;
    border-radius: 24px;
    overflow: hidden;

    :deep(.yk-input-wrapper) {
      border-radius: 24px;
      box-shadow: 0 3px 12px rgba(0, 0, 0, 0.04);
      border: 1px solid transparent;
      background-color: #f7f9fc;

      &:hover {
        background-color: #fff;
        border-color: #d9e2f2;
        box-shadow: 0 4px 14px rgba(58, 123, 213, 0.1);
      }
    }
  }

  .search-button {
    background: none;
    border: none;
    color: @primary-color;
    cursor: pointer;
    padding: 6px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;

    &:hover {
      color: @primary-dark;
      transform: scale(1.1);
    }
  }

  .cancel-button {
    display: flex;
    align-items: center;
    background: none;
    border: none;
    color: @light-text;
    cursor: pointer;
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.2s;

    &:hover {
      background-color: @bg-hover;
      color: @text-color;
    }

    .cancel-icon {
      font-size: 18px;
      margin-right: 4px;
    }
  }

  &.has-value {
    .search-input {
      width: 260px;
    }
  }

  &.is-searching {
    .search-input {
      :deep(.yk-input-wrapper) {
        background-color: #fff;
        border-color: @primary-color;
        box-shadow: 0 4px 16px rgba(58, 123, 213, 0.15);
      }
    }

    .search-button {
      color: @primary-dark;
    }
  }
}

// 响应式设计
@media (max-width: 992px) {
  .top-title {
    padding: 18px 0;
    margin-bottom: 24px;
  }
}

@media (max-width: 768px) {
  .top-title {
    flex-direction: column;
    align-items: flex-start;
    gap: 18px;

    &__right {
      width: 100%;
    }

    &::after {
      background: linear-gradient(to right, rgba(58, 123, 213, 0.5), rgba(58, 123, 213, 0.5));
    }
  }

  .search-container {
    width: 100%;

    .search-input,
    &.has-value .search-input {
      width: 100%;
    }
  }
}

@media (max-width: 480px) {
  .top-title {
    padding: 14px 0;
    margin-bottom: 18px;

    &__heading {
      font-size: 18px;
      padding-left: 12px;

      &::before {
        width: 3px;
      }
    }

    &__left {
      gap: 10px;
    }
  }

  .search-container {
    .search-input {
      :deep(.yk-input-wrapper) {
        height: 36px;

        .yk-input {
          font-size: 14px;
        }
      }
    }

    .cancel-button {
      padding: 5px 8px;

      .cancel-text {
        display: none;
      }

      .cancel-icon {
        margin-right: 0;
        font-size: 16px;
      }
    }
  }
}

// 兼容超小屏幕
@media (max-width: 320px) {
  .top-title {
    &__heading {
      font-size: 16px;
    }

    &::after {
      margin-top: 6px;
    }
  }
}
</style>